package com.etang.a2020b1019.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.etang.a2020b1019.R;
import com.etang.a2020b1019.tools.AppConfig;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends Fragment {

    private TextView tv_temp, tv_hum, tv_smo, tv_gas, tv_press, tv_ill, tv_pm, tv_co, tv_per;
    private ListView lv_link_mode;
    private SharedPreferences sharedPreferences;
    private List<String> list = new ArrayList<>();
    private ListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, null, false);
        initView(view);
        initData();
        handler.post(time);
        return view;
    }

    private void initData() {
        list.clear();
        list.add("烟雾模式：" + sharedPreferences.getString("smo_state", null));
        list.add("照度模式：" + sharedPreferences.getString("ill_state", null));
        list.add("温度模式：" + sharedPreferences.getString("temp_state", null));
        list.add("绿色模式：" + sharedPreferences.getString("per_state", null));
        list.add("PM2.5模式：" + sharedPreferences.getString("pm_state", null));
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        lv_link_mode.setAdapter(adapter);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            tv_smo.setText(String.valueOf(AppConfig.smo));
            tv_press.setText(String.valueOf(AppConfig.press));
            tv_pm.setText(String.valueOf(AppConfig.pm));
            tv_per.setText(String.valueOf(AppConfig.per));
            tv_gas.setText(String.valueOf(AppConfig.gas));
            tv_co.setText(String.valueOf(AppConfig.co));
            tv_hum.setText(String.valueOf(AppConfig.hum));
            tv_ill.setText(String.valueOf(AppConfig.ill));
            tv_temp.setText(String.valueOf(AppConfig.temp));
            initData();
            handler.postDelayed(time, 1000);
        }
    };
    Runnable time = new Runnable() {
        @Override
        public void run() {
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    private void initView(View view) {
        sharedPreferences = getActivity().getSharedPreferences("rember", Context.MODE_PRIVATE);
        lv_link_mode = (ListView) view.findViewById(R.id.lv_link_mode);
        tv_co = (TextView) view.findViewById(R.id.tv_co);
        tv_gas = (TextView) view.findViewById(R.id.tv_gas);
        tv_hum = (TextView) view.findViewById(R.id.tv_hum);
        tv_ill = (TextView) view.findViewById(R.id.tv_ill);
        tv_per = (TextView) view.findViewById(R.id.tv_per);
        tv_pm = (TextView) view.findViewById(R.id.tv_pm);
        tv_press = (TextView) view.findViewById(R.id.tv_press);
        tv_smo = (TextView) view.findViewById(R.id.tv_smo);
        tv_temp = (TextView) view.findViewById(R.id.tv_temp);
    }
}
